#include "XmlUtil.h"

const char * XmlNode::GetTagName()
{
	return value.c_str();
}

XmlNode * XmlNode::GetParent()
{
	return reinterpret_cast<XmlNode *>( parent );
}

int	XmlNode::GetChildNodeCount( const char * tagName )
{
	int count = 0;

	TiXmlNode * node = FirstChild( tagName );
	if( node != NULL )
	{
		if( node->Type() != ELEMENT )
		{
			node = node->NextSiblingElement( tagName );
		}

		while( node )
		{
			count++;
			node = node->NextSiblingElement( tagName );
		}
	}

	return count;
}

XmlNode * XmlNode::GetChildNode( const char * tagName, int idx )
{
	TiXmlNode * node = FirstChild( tagName );
	if( node == NULL )
	{
		return NULL;
	}

	if( node->Type() != ELEMENT )
	{
		node = node->NextSiblingElement( tagName );
		if( node == NULL )
		{
			return NULL;
		}
	}
	
	for( int cnt = 0; cnt < idx; cnt++ )
	{
		node = node->NextSiblingElement( tagName );
		if( node == NULL )
		{
			return NULL;
		}
	}

	return reinterpret_cast<XmlNode *>( node );
}

XmlNode * XmlNode::AddChildNode( const char * tagName )
{
	TiXmlElement element( tagName );
	XmlNode * node = reinterpret_cast<XmlNode *>( InsertEndChild( element ) );

	return node;
}

BOOL XmlNode::DelChildNode( const char * tagName, int idx )
{
	TiXmlNode * node = FirstChild( tagName );
	if( node == NULL )
	{
		return FALSE;
	}

	if( node->Type() != ELEMENT )
	{
		node = node->NextSiblingElement( tagName );
		if( node == NULL )
		{
			return FALSE;
		}
	}
	
	for( int cnt = 0; cnt < idx; cnt++ )
	{
		node = node->NextSiblingElement( tagName );
		if( node == NULL )
		{
			return FALSE;
		}
	}

	return RemoveChild( node );
}

const char * XmlNode::GetAttribute( const char * name )
{
	const TiXmlElement * element = ToElement();
	if( element == NULL )
	{
		return NULL;
	}

	return element->Attribute( name );
}

void XmlNode::SetAttribute( const char * name, const char * value )
{
	TiXmlElement * element = ToElement();
	if( element != NULL )
	{
		element->SetAttribute( name, value );
	}
}

const char * XmlNode::GetText()
{
	TiXmlNode * node = FirstChild();
	if( node == NULL )
	{
		return NULL;
	}

	const TiXmlText * text = node->ToText();

	return text->Value();
}

void XmlNode::SetText( const char * text )
{
	TiXmlText textNode( text );
	InsertEndChild( textNode );
}

/////////////////////////////////////////////////////////////////////////////////////////////

bool XmlDocument::LoadFile( const char * fileName )
{
	return TiXmlDocument::LoadFile( fileName );
}

bool XmlDocument::SaveFile( const char * fileName )
{
	return TiXmlDocument::SaveFile( fileName );
}

XmlNode * XmlDocument::GetRootNode()
{
	return (XmlNode *)this; 
}

XmlNode * XmlDocument::GetChildNode( const char * tagName, int idx )
{
	TiXmlNode * node = FirstChild( tagName );
	if( node == NULL )
	{
		return NULL;
	}

	if( node->Type() != ELEMENT )
	{
		node = node->NextSiblingElement( tagName );
		if( node == NULL )
		{
			return NULL;
		}
	}
	
	for( int cnt = 0; cnt < idx; cnt++ )
	{
		node = node->NextSiblingElement( tagName );
		if( node == NULL )
		{
			return NULL;
		}
	}

	return reinterpret_cast<XmlNode *>( node );
}

XmlNode * XmlDocument::AddChildNode( const char * tagName )
{
	TiXmlElement element( tagName );
	XmlNode * node = reinterpret_cast<XmlNode *>( InsertEndChild( element ) );

	return node;
}